const fs = require('fs')
const path = require('path')
const { promisify } = require('util')

// 目录创建之异步实现:

// 将 access 与 mkdir 处理成 promise 风格

const access = promisify(fs.access)
const mkdir = promisify(fs.mkdir)

async function promiseMakeDir(dirPath, cb) {
  let parts = dirPath.split('/')
  for (let index = 1; index <= parts.length; index++) {
    let current = parts.slice(0, index).join('/')
    try {
      await access(current)
    } catch (err) {
      await mkdir(current)
    }
  }
  cb && cb()
}

promiseMakeDir('pa/pb/pc', () => {
  console.info('创建完成!')
})
